package com.boot.project.controller;

import com.boot.project.domain.InfoDo;
import com.boot.project.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资讯的controller，如首页轮播，订场轮播，少儿轮播，
 * 首页下拉资讯以及少儿下拉资讯
 */
@RequestMapping("/info")
@RestController
public class InfoController {
    @Autowired
    private InfoService infoService;

    // 获取轮播图列表的接口，如首页轮播，订场轮播以及少儿轮播
    @GetMapping("/rotation")
    public List<?> rotation(@RequestParam Integer infoType) {
        return infoService.rotation(infoType);
    }

    // 获取下拉资讯接口，因为涉及到分页所以跟轮播分开写，如首页资讯，少儿资讯
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam Integer pageNum, Integer pageSize, Integer infoType) {
        Map<String, Object> result = new HashMap<>();
        List<InfoDo> query = infoService.list(pageNum, pageSize, infoType);
        Integer total = infoService.count(infoType);
        result.put("query", query);
        result.put("total", total);
        return result;
    }


    // 根据资讯ID来获取该资讯的全部信息
    @GetMapping("/getInfo")
    public InfoDo getInfo(@RequestParam Long id) {
        return infoService.getInfo(id);
    }

    // 新增资讯的时候上传图片的接口
    @PostMapping("/upload")
    public String upload(@RequestParam MultipartFile file, Long infoType) throws IOException {
        return infoService.upload(file, infoType);
    }


    // 新增资讯接口，这个接口是后台管理人员访问的
    @PostMapping("/saveInfo")
    public Map<String, Object> saveInfo(@RequestBody InfoDo infoDo){
        return infoService.saveInfo(infoDo);
    }

    // 资讯删除接口
    @DeleteMapping("/deleteInfo")
    public Map<String, Object> deleteInfo(@RequestParam Long id){
        return infoService.deleteInfo(id);
    }
}
